#include "Utils.h"

//*CARLO* modificata, prima dava sempre errore
bool Utils::isHexText(string s)
{
	std::string::size_type found;
	
	if(s.size()==0 || s.size()%2!=0){
		return 0;
	}
	found=s.find_first_not_of("0123456789ABCDEFabcdef");
	if (found!=string::npos){
		return 0;
	}
	return 1;
}
void Utils::HexToBin(string s,uint8_t* b)
{
	unsigned int i,j;
	char ch;
	uint8_t temp;
	
	for (i=0;i<s.size();i+=2){
		temp=0;
		for(j=0;j<=1;j++){
			ch=s.c_str()[i+j];
			if (ch >= '0' && ch <= '9'){
				temp = (temp << 4) + (ch - '0');
			}else if (ch >= 'A' && ch <= 'F'){
				temp = (temp << 4) + (ch - 'A' + 10);
			}else if (ch >= 'a' && ch <= 'f'){
				temp = (temp << 4) + (ch - 'a' + 10);
			}
		}
		b[i/2]=temp;
	}
}//+++ endianess input
string Utils::CleanString(string s){
	string temp;
	char* buf;
	unsigned int i,j;
	
	buf=new char[s.size()];
	for(i=0,j=0;i<s.size();i++){
		if(s.c_str()[i]!=' '&&s.c_str()[i]!='\r'&&s.c_str()[i]!='\n'&&
					s.c_str()[i]!='\t'){
			buf[j]=s.c_str()[i];
			j++;
		}
	}
	buf[j]='\0';
	temp=string(buf);
	delete buf;
	return temp;
}
vector<string> Utils::getDirFiles(string dir)
{
	vector<string> files;
	string temp;
	
	DIR *dp;
	struct dirent *dirp;
	if((dp  = opendir(dir.c_str())) == NULL) {
		throw(EXC_FILE_NOT_FOUND);
	}
	while ((dirp = readdir(dp)) != NULL) {
		temp = string(dirp->d_name);
		if(temp.compare(".")&&temp.compare("..")){
			files.push_back(string(dirp->d_name));
		}
	}
	sort(files.begin(),files.end());
	closedir(dp);
	return files;
}//+++ numerical sort: file 10000




